package com.cool4j.generator.util

import com.cool4j.generator.exception.ExceptionMessage
import com.cool4j.view.Activator
import org.apache.commons.lang3.StringUtils
import org.eclipse.core.filesystem.EFS
import org.eclipse.core.runtime.IStatus
import org.eclipse.core.runtime.Path
import org.eclipse.core.runtime.Status
import org.eclipse.jface.dialogs.MessageDialog
import org.eclipse.ui.IWorkbenchPage
import org.eclipse.ui.IWorkbenchWindow
import org.eclipse.ui.PartInitException
import org.eclipse.ui.ide.IDE

import java.text.MessageFormat

/**
 * 系统运行时日志记录和读取类
 *
 * @author Frank create on 16/1/8 10:56
 */
class LogUtil {

    private static def logFile

    /**
     * 保存异常日志信息
     *
     * @param exception - 异常对象
     */
    static void logging(Exception exception) {
        if (StringUtils.isBlank(logFile)) {
            initLogFile()
        }
        def printWriter
        try {
            printWriter = new PrintWriter(new FileWriter(logFile, true))
            exception.printStackTrace(printWriter)
            printWriter.flush()
            printWriter.close()
        } catch (IOException e) {
            saveLog(e, ExceptionMessage.LOG_SAVE_LOG_EXCEPTION)
        }
    }

    /**
     * 保存异常日志信息
     *
     * @param message - 消息
     * @param exception - 异常对象
     */
    static void logging(String message, Exception exception) {
        if (StringUtils.isBlank(logFile)) {
            initLogFile()
        }
        def printWriter
        try {
            printWriter = new PrintWriter(new FileWriter(logFile, true))
            printWriter.println(message + ":")
            exception.printStackTrace(printWriter)
            printWriter.flush()
            printWriter.close()
        } catch (IOException e) {
            saveLog(e, ExceptionMessage.LOG_SAVE_LOG_EXCEPTION)
        }
    }

    /**
     * 初始化日志文件路径
     */
    static void initLogFile() {
        logFile = Activator.getDataFolder() + "log.log"
        GeneratorUtil.createFileOrFolder(logFile, true)
    }

    /**
     * 清空日志文件
     */
    static void clearLogFile() {
        if (StringUtils.isBlank(logFile)) {
            initLogFile()
        }
        def file = new File(logFile)
        if (file.exists() && file.isFile()) {
            file.delete()
        }
        try {
            file.createNewFile()
        } catch (IOException e) {
            saveLog(e, ExceptionMessage.LOG_CLEAN_LOG_EXCEPTION)
        }
    }

    /**
     * 读取日志信息
     *
     * @param window - 窗口
     */
    static void readLog(IWorkbenchWindow window) {
        if (StringUtils.isBlank(logFile)) {
            initLogFile()
        }
        def fileStore = EFS.getLocalFileSystem().getStore(new Path(logFile))
        if (!fileStore.fetchInfo().isDirectory() && fileStore.fetchInfo().exists()) {
            IWorkbenchPage page = window.getActivePage()
            try {
                IDE.openEditorOnFileStore(page, fileStore)
            } catch (PartInitException e) {
                e.printStackTrace()
            }
        } else {
            def msgFileNotExist = ExceptionMessage.LOG_LOG_FILE_NOT_EXIST
            def msg = MessageFormat.format(msgFileNotExist, [fileStore.getName()])
            MessageDialog.openWarning(window.getShell(), ExceptionMessage.LOG_WARN, msg)
        }
    }

    private static void saveLog(Exception exception, String message) {
        def status = new Status(Status.ERROR, Activator.PLUGIN_ID, message, exception)
        Activator.getDefault().getLog().log(status)
    }

}
